// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PollServiceImp.java

package com.iphaeton.lms.business.poll;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.poll.Option;
import com.iphaeton.lms.pojo.poll.Poll;
import com.iphaeton.persistence.IPersistenceService;
import java.util.*;
import org.apache.log4j.Logger;

// Referenced classes of package com.iphaeton.lms.business.poll:
//            IPollService

public class PollServiceImp extends PersistentServiceSupport
    implements IPollService
{

    public PollServiceImp()
    {
    }

    public void add(Poll poll)
        throws BusinessException
    {
        try
        {
            hbpservice.store(poll);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u53D1\u5E03\u6295\u7968\u5931\u8D25!");
        }
    }

    public List queryPollOption(String id)
        throws BusinessException
    {
        List list = new LinkedList();
        String hql = "from Option option where option.poll_id='" + id + "' order by option.id";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u8BE2\u6295\u7968\u9009\u9879\u5931\u8D25!");
        }
        return list;
    }

    public List queryPoll()
        throws BusinessException
    {
        List list = null;
        String hql = "from Poll poll where poll.valid=1 order by poll.create_time desc";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u6295\u7968\u5931\u8D25!");
        }
        return list;
    }

    public List showPoll()
    {
        List list = new LinkedList();
        String hql = "from Poll poll order by poll.create_time desc ";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    public String count(Option option)
        throws BusinessException
    {
        String count = null;
        count = (new StringBuffer(String.valueOf(option.getCount().intValue() + 1))).toString();
        option.setCount(new Integer(count));
        try
        {
            hbpservice.store(option);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BA1\u7B97\u6295\u7968\u4EBA\u6570\u5931\u8D25!");
        }
        return count;
    }

    public Poll findPollById(String id)
    {
        Poll rep = null;
        try
        {
            rep = (Poll)hbpservice.get(id, com.iphaeton.lms.pojo.poll.Poll.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
        }
        return rep;
    }

    public void addOption(Option option)
        throws BusinessException
    {
        try
        {
            hbpservice.store(option);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u6295\u7968\u7B54\u6848\u5230\u6570\u636E\u5E93\u5931\u8D25!");
        }
    }

    public List getSpecifiedPoll(Pagination pageInfo)
        throws BusinessException
    {
        return getSpecifiedItems(pageInfo);
    }

    public void valid(String pollid)
        throws BusinessException
    {
        Poll poll = null;
        try
        {
            poll = (Poll)hbpservice.get(pollid, com.iphaeton.lms.pojo.poll.Poll.class);
            Integer valid = poll.getValid();
            if(valid.intValue() == 0)
            {
                valid = new Integer(1);
                List pollList = showPoll();
                for(Iterator it = pollList.iterator(); it.hasNext();)
                {
                    Poll tmpObj = (Poll)it.next();
                    String id = tmpObj.getId();
                    if(!id.equals(pollid))
                    {
                        tmpObj.setValid(new Integer(0));
                        hbpservice.store(tmpObj);
                    }
                }

                poll.setValid(valid);
                hbpservice.store(poll);
            }
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u6295\u7968\u72B6\u6001\u5931\u8D25!");
        }
    }

    public void open(String pollid)
        throws BusinessException
    {
        Poll poll = null;
        try
        {
            poll = (Poll)hbpservice.get(pollid, com.iphaeton.lms.pojo.poll.Poll.class);
            Integer open = poll.getState();
            if(open.intValue() == 0)
            {
                open = new Integer(1);
                poll.setState(open);
            } else
            {
                open = new Integer(0);
                poll.setState(open);
            }
            hbpservice.store(poll);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("!");
        }
    }

    public void removePoll(Poll p)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(p);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u6295\u7968\u5931\u8D25!");
        }
    }

    public Pagination findPoll(int pageSize)
        throws BusinessException
    {
        String hql = "select count(poll) from Poll poll";
        String hqlQueryStr = "from Poll poll  order by poll.create_time desc ";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5206\u9875\u67E5\u8BE2\u6295\u7968\u5931\u8D25!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.poll.PollServiceImp.class.getName());
    }
}
